package com.yisu.expressway.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import com.yisu.expressway.R;

public class TitlebarLayout extends FrameLayout {
    View rootView;
    ImageButton mBackBtn;
    TextView mCancelTv;
    TextView mTitleTv;
    TextView mActionTv;
//    ImageView mActionIv;
//    ImageView mactionCol;
    ImageButton mRightIv;
    View mUnderLine;
    private Context context;

    private void inject(){
        rootView = findViewById(R.id.rootView);
        mBackBtn = (ImageButton)findViewById(R.id.backBtn);
        mCancelTv = (TextView)findViewById(R.id.cancelTv);
        mTitleTv = (TextView)findViewById(R.id.titleTv);
        mActionTv = (TextView)findViewById(R.id.actionTv);
//        mActionIv = (ImageView)findViewById(R.id.actionIv);
//        mactionCol=(ImageView)findViewById(R.id.actionCol);
        mRightIv = (ImageButton)findViewById(R.id.iv_btn_right);
        mUnderLine = findViewById(R.id.subTitleLiner);

    }
    public void setRightBtnCorlor(int i){
        mActionTv.setTextColor(getContext().getResources().getColor(i));
    }

    public void setLeftImage(int id){
        mBackBtn.setImageResource(id);
    }

//    public void setRightImage(int id){
//        mRightIv.setVisibility(View.VISIBLE);
//        mRightIv.setImageResource(id);
//    }

//    public void setCollectImage(int id){
//        mactionCol.setImageResource(id);
//    }

    public TitlebarLayout(Context context) {

        super(context);
        this.context = context;
        init();
    }

    public TitlebarLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public TitlebarLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        init();
    }

    private void init() {
        inflate(getContext(), R.layout.title_bar, this);
        inject();
    }

    public void transparentBg(){
        rootView.setBackgroundResource(0);
        mUnderLine.setVisibility(View.INVISIBLE);
    }

    /**
     * 设置标题
     * @param title
     */
    public void setTitle(String title) {
        if (title != null)
            mTitleTv.setText(title);
    }

    /**
     * 设置标题
     * @param stringId string id
     */
    public void setTitle(int stringId) {
            mTitleTv.setText(stringId);
    }


    /**
     * 设置标题及颜色
     * @param title
     * @param color
     */
    public void setTitle(String title, int color) {
        if (title != null) {
            mTitleTv.setText(title);
            mTitleTv.setTextColor(color);
        }
    }

    public void setTitle(String title,OnClickListener clickBackListener) {
        setTitle(title, false,"",false, clickBackListener);
    }
    public void setTitleWithCancel(String title,OnClickListener clickBackListener) {
        setTitle(title, true, getContext().getString(R.string.action_cancel), false, clickBackListener);
    }
    public void setClickBackListener(OnClickListener clickListener){
        mBackBtn.setOnClickListener(clickListener);
    }

    private void setTitle(String title,boolean isLeftAction, String leftName,boolean isDelete, OnClickListener clickLeftListener) {
        if (title != null)
            mTitleTv.setText(title);
        View target = null;
        if(isLeftAction){
            mBackBtn.setVisibility(GONE);
            mCancelTv.setVisibility(VISIBLE);
            mCancelTv.setText(leftName);
            if(isDelete){
                //mCancelTv.setTextAppearance(getContext(),R.style.aha_action_name);
            }
            target = mCancelTv;
        }else{
            mBackBtn.setVisibility(VISIBLE);
            mCancelTv.setVisibility(GONE);
            target = mBackBtn;

        }
        if (clickLeftListener != null){
            target.setOnClickListener(clickLeftListener);
        }
    }



    public void setActionBtn(String actionTxt, OnClickListener clickActionListener) {
/*        if(!TextUtils.isEmpty(actionTxt)) {
            mActionTv.setVisibility(VISIBLE);
            mActionTv.setText(actionTxt);
        }
        mActionIv.setVisibility(GONE);
        if (clickActionListener != null) {
            mActionTv.setOnClickListener(clickActionListener);
        }*/
    }

    public void setActionBtn(int actionIconResId, OnClickListener clickActionListener) {

/*        mActionTv.setVisibility(GONE);
        if(actionIconResId > 0){
            mActionIv.setVisibility(VISIBLE);
            mActionIv.setImageResource(actionIconResId);
        }
        if (clickActionListener != null) {
            mActionIv.setOnClickListener(clickActionListener);
        }*/
    }
    public void setColBtn(int actionIconResId) {

/*        mactionCol.setVisibility(GONE);
        if(actionIconResId > 0){
            mactionCol.setVisibility(VISIBLE);
            mactionCol.setImageResource(actionIconResId);
        }*/
    }

    public void setRightButtonAction(int iconId, OnClickListener clickActionListener) {

        if(iconId > 0){
            mRightIv.setVisibility(VISIBLE);
            mRightIv.setImageResource(iconId);
        }
        if (clickActionListener != null) {
            mRightIv.setOnClickListener(clickActionListener);
        }
    }

    public void setActionBtnEnable(boolean enable) {
        mActionTv.setEnabled(enable);
    }

    public void hideActionBtn(){
        mActionTv.setVisibility(GONE);
//        mActionIv.setVisibility(GONE);
    }

    public View getBottomLine(){
        return mUnderLine;
    }

    public void hideBackBtn(){
        mBackBtn.setVisibility(GONE);
    }
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

}
